<!-- 打开方式 -->

<template>
  <div class="MenuList-MyOpen">
    <FormRadio label="打开方式" bind="openType" :dict="DICT.TYPE" required @change="changeOpenType" />

    <FormTree
      v-if="one.openType === 1"
      label="目录"
      bind="catalogue"
      :dict="route_treeDict"
      :grayed="GETTER.grayedCatalogue"
      :disabled="!menu_readySelect"
      popClass="MenuList-MyOpen-catalogue"
      hasCount
      hasRight
      onlyLeaf
      allName
      required
    />
    <FormInput
      v-else-if="one.openType === 2"
      label="目录"
      bind="catalogue"
      tip="形如 XxxYyy 或 XxxYyyZzz (即路径为 /xxx/yyy 或 /xxx/yyy/zzz)"
      :max="20"
      :check="checkFrame"
      required
    />
    <FormInput v-else label="目录" bind="catalogue" disabled />

    <FormLink label="链接" bind="landingPage" :disabled="one.openType === 1" required />
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyOpen',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [
    CORE.mixWeb(),
    CORE.mixTree('route', 'route_'),
    CORE.mixSelect('menu', 'menu_'),
  ],

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 切换打开方式
     */
    changeOpenType(type) {
      if (type === 1) this.one.landingPage = ''

      this.one.catalogue = ''
    },

    /**
     * 检查内嵌目录
     */
    checkFrame(value) {
      if (!/^[A-Za-z]+$/.test(value)) return '只能字母'
      if (!/^([A-Z][a-z]+){2,3}$/.test(value)) return '形如 XxxYyy 或 XxxYyyZzz'

      const { menu_select, one: { id } } = this
      const has = menu_select.some((item) => item.id !== id && item.catalogue === value)
      return has ? '该目录已存在' : ''
    },
  },
}
</script>

<style lang="scss">
@import './index.scss';
</style>
